// components/calendar/calendar.js
const {formatTime
} = require("../../utils/util")
Component({
	properties: {
		currentDate: String,
		taskDateList: Array,
		showheader: Boolean,
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		dateList: [], //日历主体渲染数组
		selectDay: {}, //选中时间
		diaryList: [],
		showheader:true,
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		dateInit() {
			const {
				year,
				month,
				day
			} = this.data.selectDay

			let {
				chooseDay,
				taskDateList
			} = this.data

			let dateList = []; //需要遍历的日历数组数据

			let now = new Date(year, month - 1) //当前月份的1号
			let startWeek = now.getDay(); //目标月1号对应的星期
			let dayNum = new Date(year, month, 0).getDate() //当前月有多少天
			let forNum = Math.ceil((startWeek + dayNum) / 7) * 7 //当前月跨越的周数
		
			for (let i = 0; i < forNum; i++) {
				const now2 = new Date(now)
				const day = now2.getDate()
				now2.setDate(i - startWeek + 1)
				let obj = {};
				obj = {
					day: now2.getDate(),
					month: now2.getMonth() + 1,
					year: now2.getFullYear(),
					hasDiary: false,
					isVacant:false,
					isSelect: false,
					dateString: formatTime(now2, "Y-M-D")
				};
				dateList[i] = obj;
			}

			for (const item of dateList) {
				for (const pathItem of taskDateList) {
					const releaseDay = pathItem.taskDate
					if (releaseDay === item.dateString) {
						item.hasDiary = true
						if(pathItem.isVacant === 1){
							item.isVacant = true
						}
					}
				}
			}
			this.setData({
				dateList: dateList
			})
		},
	},

	lifetimes: {
		attached() {
			let arr = this.data.currentDate.split(".")
			let now = this.data.today ? new Date(this.data.today) : new Date()
			let selectDay = {
				year: Number(arr[0]),//  now.getFullYear(),
				month: Number(arr[1]), // now.getMonth() + 1,
				day: 1 ,// now.getDate(),
				dateString: formatTime(now, "Y-M-D")
			}
			this.setData({
				selectDay,
				chooseDay: selectDay.dateString
			})
			this.dateInit(selectDay.year, selectDay.month, selectDay.day)
		}
	},

})
